/* eslint-disable handle-callback-err */
/* eslint-disable handle-callback-err */
<template>
  <div class="app-container">
    <search-pannel
      v-model="element"
      :btn-items="btnItems"
      @searchmethod="search"
      @searchcondition="generatecondition"
      @create="create($event)"
    />
    <CreateOrUpdate
      v-if="createOrUpdateVisible"
      ref="createorupdate"
      @refreshDataList="getDataList()"
    />
    <AssignMenu v-if="assignMenuVisible" ref="assignMenu" @refreshDataList="getDataList()" />
    <AssignMenuData v-if="assignMenuDataVisible" ref="assignMenuData" @refreshDataList="getDataList()" />
  </div>
</template>
<script>
import SearchPannel from '@/components/SearchPannel'
import CreateOrUpdate from './components/CreateOrUpdate'
import AssignMenuData from './components/AssignMenuData'
import AssignMenu from './components/AssignMenu'
import { fetchAll } from '@/api/tenant'
import { fetchList, deleteRole } from '@/api/role'
export default {
  components: {
    SearchPannel,
    CreateOrUpdate,
    AssignMenu,
    AssignMenuData
  },
  data() {
    return {
      tenantList: [],
      createOrUpdateVisible: false,
      assignMenuVisible: false,
      assignMenuDataVisible: false,
      element: {
        // 规划后参数
        search: {
          // 普通查询的结果,用来生成查询条件
          searchresult: ''
        },
        // 高级查询
        plussearch: {
          // 是否显示
          visible: false
        },
        btnItems: {

        },
        tableOptions: {

        },
        // 以前参数

        pageIndex: 1,
        pageSize: 10,
        total: 1,
        totalPage: 1,
        dataList: [],
        grid: {
          normalitems: [{
            prop: 'Name',
            label: '名称'
          }, {
            prop: 'Code',
            label: '代码'
          }, {
            prop: 'TenantName',
            label: '租户'
          }],
          operationitem: [{
            name: '修改',
            icon: 'el-icon-edit',
            method: (row) => {
              this.ModifyHandler(row)
            }
          }, {
            name: '删除',
            icon: 'el-icon-delete',
            method: (row) => {
              this.DeleteHandler(row)
            }
          }, {
            name: '菜单',
            icon: 'el-icon-setting',
            method: (row) => {
              this.AssignMenuHandler(row)
            }
          }, {
            name: '数据权限',
            icon: 'el-icon-user',
            method: (row) => {
              this.AssignMenuDataHandler(row)
            }
          }]
        },
        filters: {
          isShow: false
        }
      }
    }
  },
  mounted() {
    // 获取租户
    fetchAll().then((data) => {
      if (data && data.code === '20000') {
        const result = data.data
        this.tenantList = result
      }
    }).then(() => {
      this.getDataList()
    })
  },
  methods: {
    getDataList() {
      const condition = {
        limit: this.element.pageSize,
        offset: this.element.pageSize * (this.element.pageIndex - 1)
      }
      const searchcondition = this.element.searchresult
      if (searchcondition) {
        condition.query = `Code.contains:${searchcondition},Condition_Join:or,Name.contains:${searchcondition}`
      }
      fetchList(condition).then((data) => {
        if (data.data && data.data.length > 0) {
          data.data.forEach((item, index) => {
            const re = this.tenantList.filter(r => r.Id === item.Tenant)
            if (re && re.length > 0) {
              item.TenantName = re[0].Name
            } else {
              item.TenantName = ''
            }
          })
          this.element.dataList = data.data
          this.element.totalPage = data.count % this.element.pageSize === 0 ? (data.count / this.element.pageSize) : (Math.floor(data.count / this.element.pageSize) + 1)
          this.element.total = data.count
        } else {
          this.element.dataList = []
        }
      })
    },
    search() {
      this.getDataList()
    },
    designHandler(row) {
      this.designvisible = true
      this.$nextTick(() => {
        this.$refs.editprocessmodel.init(row)
      })
    },
    AssignMenuHandler(row) {
      this.assignMenuVisible = true
      this.$nextTick(() => {
        this.$refs.assignMenu.init(row.Id, row.Code)
      })
    },
    AssignMenuDataHandler(row) {
      this.assignMenuDataVisible = true
      this.$nextTick(() => {
        this.$refs.assignMenuData.init(row.Id, row.Code)
      })
    },
    ModifyHandler(row) {
      this.createOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.createorupdate.init(row.Id)
      })
    },
    create() {
      this.createOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.createorupdate.init()
      })
    },
    DeleteHandler(row) {
      const id = row.Id
      this.$confirm(`确定对[id=${id}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteRole(id).then((data) => {
          if (data && data.code === '20000') {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.message)
          }
        })
      }).catch(() => {})
    }
  }
}
</script>
